package dynamicdiscountsystem;

import java.sql.ResultSet;
import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author atilla1991
 */
public class DiscountOperations {

    public double discount1DiscountPercent;
    public double discount2LimitPrice;
    public double discount2DiscountPercent;
    public int discount3LimitProductNumber;
    public int discount3PayingProductNumber;
    DatabaseConnection con = new DatabaseConnection();

    public void getDiscounts() {

        try {
            ResultSet rs = null;

            rs = con.ExecuteQuery("SELECT DISCOUNT_PERCENT FROM DISCOUNT1 "
                    + "ORDER BY idDISCOUNT1 DESC LIMIT 1");

            if (rs.next()) {
                discount1DiscountPercent = rs.getDouble("DISCOUNT_PERCENT");
            }
            rs = con.ExecuteQuery("SELECT LIMIT_PRICE,DISCOUNT_PERCENT "
                    + "FROM DISCOUNT2 ORDER BY idDISCOUNT2 DESC LIMIT 1");

            if (rs.next()) {
                discount2LimitPrice = rs.getDouble("LIMIT_PRICE");
                discount2DiscountPercent = rs.getDouble("DISCOUNT_PERCENT");
            }
            rs = con.ExecuteQuery("SELECT LIMIT_PRODUCT_NUMBER,PAYING_PRODUCT_NUMBER "
                    + "FROM DISCOUNT3 ORDER BY idDISCOUNT3 DESC LIMIT 1");

            if (rs.next()) {
                discount3LimitProductNumber = rs.getInt("LIMIT_PRODUCT_NUMBER");
                discount3PayingProductNumber = rs.getInt("PAYING_PRODUCT_NUMBER");
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }

    }

    public double discount1Application(double productSaleCost, int productCount) {

        return productSaleCost * productCount * (100.0 - discount1DiscountPercent) / 100.0;
    }

    public double discount2Application(double productSaleCost, int productCount) {

        if (productSaleCost * productCount >= discount2LimitPrice) {
            return productSaleCost * productCount * (100.0 - discount2DiscountPercent) / 100.0;
        } else {
            return productSaleCost * productCount;
        }

    }

    public double discount3Application(double productSaleCost, int productCount) {

        if (productCount >= discount3LimitProductNumber) {
            double discountedPrice =
                    (int) (productCount / discount3LimitProductNumber)
                    * discount3PayingProductNumber * productSaleCost;
            double unDiscountedPrice = (productCount % discount3LimitProductNumber) * productSaleCost;
            return discountedPrice + unDiscountedPrice;

        } else {
            return productSaleCost * productCount;
        }

    }

    public double minValue(double[] prices) {
        Arrays.sort(prices);
        return prices[0];
    }

    public double minPrice(int idProduct, double productSaleCost, int productCount) {

        double[] discountedPrice = new double[3];
        try {
            ResultSet rs;
            //
            rs = con.ExecuteQuery("SELECT * FROM DISCOUNT_APPLICATION "
                    + "WHERE idPRODUCT_INPUT = " + idProduct + " AND DISCOUNT_NUMBER = " + 1);
            if (rs.next()) {
                discountedPrice[0] = discount1Application(productSaleCost, productCount);
            } else {
                discountedPrice[0] = productCount * productSaleCost;
            }

            //
            rs = con.ExecuteQuery("SELECT * FROM DISCOUNT_APPLICATION "
                    + "WHERE idPRODUCT_INPUT = " + idProduct + " AND DISCOUNT_NUMBER = " + 2);
            if (rs.next()) {
                discountedPrice[1] = discount2Application(productSaleCost, productCount);
            } else {
                discountedPrice[1] = productCount * productSaleCost;
            }

            //
            rs = con.ExecuteQuery("SELECT * FROM DISCOUNT_APPLICATION "
                    + "WHERE idPRODUCT_INPUT = " + idProduct + " AND DISCOUNT_NUMBER = " + 3);
            if (rs.next()) {
                discountedPrice[2] = discount3Application(productSaleCost, productCount);
            } else {
                discountedPrice[2] = productCount * productSaleCost;
            }
        } catch (Exception ex) {

            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        } finally {
            return minValue(discountedPrice);
        }

    }

    public double[] getInfo(DefaultTableModel model) {
        getDiscounts();
        double totalPrice = 0.0;

        int rowCount = model.getRowCount();
        double[] productSalePrices = new double[rowCount + 1];

        int i = 0;
        for (i = 0; i < rowCount; i++) {

            if (model.getValueAt(i, 6) != null && ((Integer) model.getValueAt(i, 6) <= (Integer) model.getValueAt(i, 3))) {
                productSalePrices[i] = minPrice((Integer) model.getValueAt(i, 0),
                        (Double) model.getValueAt(i, 5), (Integer) model.getValueAt(i, 6));
                totalPrice += productSalePrices[i];
                /*JOptionPane.showMessageDialog(null,
                        productSalePrices[i]);*/
            } else {
                productSalePrices[rowCount] = -1;
                return productSalePrices;
            }

        }
        //Array-in sonuncu elementi umumi meblegi gosterir.
        productSalePrices[i] = totalPrice;
        return productSalePrices;

    }
}
